package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.starter.redisson.properties.BloomFilterProperties;
import com.atguigu.starter.redisson.properties.BloomProperty;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/6 14:39
 */
@Service
public class SpuInfoServiceImpl
        extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {
    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private SpuImageService spuImageService;
    @Autowired
    private SpuSaleAttrService spuSaleAttrService;
    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;


    @Transactional
    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        //2.保存基础信息到spu_info
        int insert = spuInfoMapper.insert(spuInfo);
        //提取自增id
        Long id = spuInfo.getId();
        //2.保存信息到spu_images
        List<SpuImage> imageList = spuInfo.getSpuImageList().stream().map((ele) -> {
            ele.setSpuId(id);
            return ele;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(imageList);
        //3.保存销售属性名信息
        List<SpuSaleAttr> saleAttrList = spuInfo.getSpuSaleAttrList().stream().map((ele) -> {
            ele.setSpuId(id);
            return ele;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(saleAttrList);
        //4.保存销售属性名的所有值
        List<SpuSaleAttrValue> SaleAttrValueList = spuInfo.getSpuSaleAttrList().stream().
                flatMap((ssa) -> {
                    return ssa.getSpuSaleAttrValueList().stream().map((ssav) -> {
                        ssav.setSpuId(id);
                        ssav.setSaleAttrName(ssa.getSaleAttrName());
                        return ssav;
                    });
                }).collect(Collectors.toList());


        spuSaleAttrValueService.saveBatch(SaleAttrValueList);
    }

}
